package com.hongyuan.coach.ui.home.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import com.hongyuan.coach.R;
import com.hongyuan.coach.base.AppViewModelFactory;
import com.hongyuan.coach.base.CustomActivity;
import com.hongyuan.coach.data.Constants;
import com.hongyuan.coach.databinding.ActivityEditTimeBinding;
import com.hongyuan.coach.myview.bubble_frame.BubblePopupWindow;
import com.hongyuan.coach.myview.select_time.ContentTimeBean;
import com.hongyuan.coach.ui.home.adapter.EditTimeTabAdapter;
import com.hongyuan.coach.ui.home.adapter.TimeAdapter;
import com.hongyuan.coach.ui.home.view_mode.EditTimeViewMode;
import com.hongyuan.coach.utils.DividerItemDecoration;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.BR;

public class EditTimeActivity extends CustomActivity<ActivityEditTimeBinding, EditTimeViewMode> {

    private EditTimeTabAdapter tabAdapter;
    private TimeAdapter timeAdapter;

    //气泡弹框
    private BubblePopupWindow topWindow;
    private LayoutInflater layoutInflater;
    private View bubbleView;
    private TextView tvText;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_edit_time;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public EditTimeViewMode initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(EditTimeViewMode.class);
    }

    @Override
    public void initView() {
        super.initView();

        setOnRefresh(binding.refresh,REFRESH_0X4);
        binding.comBack.setOnClickListener(view -> finish());

        layoutInflater = LayoutInflater.from(this);
        topWindow = new BubblePopupWindow(this);


        LinearLayoutManager tabManager = new LinearLayoutManager(this);
        tabManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        binding.tabRecyc.addItemDecoration(new DividerItemDecoration(
                this, DividerItemDecoration.VERTICAL_LIST,100,0x00000000));
        binding.tabRecyc.setLayoutManager(tabManager);
        tabAdapter = new EditTimeTabAdapter();
        binding.tabRecyc.setAdapter(tabAdapter);
        tabAdapter.setOnItemChildClickListener((adapter, view1, position) -> {
            viewModel.changeTab(position);
            viewModel.nowData.set(viewModel.topTimes.get().get(position).getNow_day());

            viewModel.reqTimes(viewModel.nowData.get());
        });

        GridLayoutManager layoutManager = new GridLayoutManager(this, 4);
        binding.contentRecyc.setLayoutManager(layoutManager);
        timeAdapter = new TimeAdapter();
        binding.contentRecyc.setAdapter(timeAdapter);
        timeAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            for(ContentTimeBean dataBean : viewModel.contentTimes.get()){
                dataBean.setSelect(false);
            }
            viewModel.contentTimes.get().get(position).setSelect(true);
            adapter.setNewData(viewModel.contentTimes.get());
            showPopoWind(view,position);
        });
    }

    @Override
    public void initData() {
        super.initData();

        viewModel.requestData(Constants.GET_PLAN_DATA);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        viewModel.uc.changeTop.observe(this, topTimeBeans -> {
            tabAdapter.setNewData(topTimeBeans);
        });

        viewModel.uc.changeContent.observe(this, contentTimeBeans -> {
            timeAdapter.setNewData(contentTimeBeans);
            isRefresh = false;
        });
    }

    private boolean isRefresh;

    /*
     * 展示气泡弹框
     * */
    @SuppressLint("ClickableViewAccessibility")
    private void showPopoWind(View view, int position){
        //加载popowindow
        if(bubbleView == null){
            bubbleView = layoutInflater.inflate(R.layout.popu_content_text, null);
            topWindow.setBubbleView(bubbleView);//添加内容
            tvText = bubbleView.findViewById(R.id.tvText);
            //消失监听听
            topWindow.setOnDismissListener(() -> {
                List<ContentTimeBean> mlist = timeAdapter.getData();
                for(ContentTimeBean dataBean:mlist){
                    dataBean.setSelect(false);
                }
                if(!isRefresh){
                    timeAdapter.setNewData(mlist);
                }
            });
        }
        tvText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //用来屏蔽关闭刷新数据这步
                isRefresh = true;
                topWindow.dismiss();
                if(timeAdapter.getData().get(position).getIs_kong() == 0){
                    viewModel.clearParams().setParams("ct_id",String.valueOf(timeAdapter.getData().get(position).getCt_id()))
                            .requestData(Constants.DEL_COACH_TIMEPLAN);
                }else{
                    viewModel.clearParams().setParams("start_time",viewModel.getSelectDT(position))
                            .setParams("end_time",viewModel.getSelectDT(position+1))
                            .requestData(Constants.ADD_COACH_TIMEPLAN);
                }
            }
        });

        if(timeAdapter.getData().get(position).getIs_kong() == 1){
            tvText.setText("休息");
        }else{
            tvText.setText("没课");
        }

        topWindow.editTimeshow(view);
    }



}
